package com.jxd.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxd.dao.TicketInfoMapper;
import com.jxd.model.Employee;
import com.jxd.model.TicketInfo;
import com.jxd.service.ITicketInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TicketInfoServiceImpl
        extends ServiceImpl<TicketInfoMapper, TicketInfo> implements ITicketInfoService {

    @Autowired
    private TicketInfoMapper ticketInfoMapper;

    @Override
    public Map<String, Object> getAllTicketInfoList(Map<String, String> queryMap) {
        String page = queryMap.get("page");
        String limit = queryMap.get("limit");
        String departurePoint = queryMap.get("departurePoint");
        String arrivalLocation = queryMap.get("arrivalLocation");
        int empno = Integer.parseInt(queryMap.get("empno"));

        Map<String, Object> map = new HashMap<>(4);
        if (page != null && limit != null) {
            IPage<TicketInfo> pages = new Page<>(Integer.parseInt(page), Integer.parseInt(limit));
            IPage<TicketInfo> pageResult = ticketInfoMapper.getAllTicketInfoList(pages, departurePoint, arrivalLocation, empno);
            map.put("count", pageResult.getTotal());
            map.put("data", pageResult.getRecords());
            map.put("code", 0);
            return map;
        } else {
            map.put("data", (Object) null);
            map.put("code", "500");
            map.put("msg", "参数不符");
            map.put("count", (Object) null);
            return map;
        }
    }

    @Override
    public Map<String, Object> getAllTicketInfoList01(Map<String, String> queryMap) {
        String page = queryMap.get("page");
        String limit = queryMap.get("limit");
        int empno = Integer.parseInt(queryMap.get("empno"));

        Map<String, Object> map = new HashMap<>(4);
        if (page != null && limit != null) {
            IPage<TicketInfo> pages = new Page<>(Integer.parseInt(page), Integer.parseInt(limit));
            IPage<TicketInfo> pageResult = ticketInfoMapper.getAllTicketInfoList01(pages, empno);
            map.put("count", pageResult.getTotal());
            map.put("data", pageResult.getRecords());
            map.put("code", 0);
            return map;
        } else {
            map.put("data", (Object) null);
            map.put("code", "500");
            map.put("msg", "参数不符");
            map.put("count", (Object) null);
            return map;
        }
    }

    @Override
    public boolean updateTicketInfoByIds(Map<String, Object> updateTickets) {
        System.out.println(updateTickets.get("ticketIds").getClass());
        ArrayList<Integer> ticketIds = (ArrayList<Integer>) updateTickets.get("ticketIds");

        //int size = ticketIds.size();
        //Integer[] ticketIds01 = ticketIds.toArray(new Integer[size]);


        int[] ticketIds01 = ticketIds.stream().mapToInt(Integer::valueOf).toArray();


        //int[] ticketIds = (int[]) updateTickets.get("ticketIds");
        int travelInfoId = (int) updateTickets.get("travelInfoId");
        return ticketInfoMapper.updateTicketInfoByIds(ticketIds01, travelInfoId);
    }

    @Override
    public TicketInfo getMaxIdTicketInfo(int id) {
        return ticketInfoMapper.getMaxIdTicketInfo(id);
    }
}
